package com.lbs.demo.services.MusicCrawler;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import com.lbs.demo.entities.SongsEntity;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;


import java.util.ArrayList;
import java.util.List;

public class NetEaseCloudMusicCrawler implements PageProcessor {

    public static List<SongsEntity> topList;

    private Site site = Site.me().setRetryTimes(3).setSleepTime(200)
            .setUserAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3").setCharset("UTF-8");

    @Override
    public void process(Page page) {
        String jsonStr = page.getHtml().xpath("//textarea[@style='display:none;']/text()").toString();
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = (JsonArray) parser.parse(jsonStr);//歌曲数组
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();//每一首歌曲
            SongsEntity song = new SongsEntity();
            song.setSrc("163");//歌曲来源
            song.setSongId(jsonObject.get("privilege").getAsJsonObject().get("id").getAsString());//歌曲编号
            song.setName(jsonObject.get("album").getAsJsonObject().get("name").getAsString());//歌曲名称
            String songSinger = "";//歌手
            for (int t = 0; t < jsonObject.get("artists").getAsJsonArray().size(); t++) {
                if (t > 0) songSinger = songSinger.concat(" & ");
                songSinger = songSinger.concat(jsonObject.get("artists").getAsJsonArray().get(t).getAsJsonObject().get("name").getAsString());//每一个歌手
            }
            song.setSinger(songSinger);
            song.setDuration(jsonObject.get("duration").getAsInt());//歌曲时长（毫秒）
            topList.add(song);
//           System.out.println("歌曲编号:" + song.getSongId() + " 名称：" + song.getName() + " 时长：" + song.getDuration() + " 歌手：" + song.getSinger());
        }

//        List<String> name = page.getHtml().xpath("//ul[@class='f-hide']/li/a/text()").all();
//        List<String> link = page.getHtml().xpath("//ul[@class='f-hide']/li").links().all();
//        for (int i = 0; i < name.size() && i < link.size(); i++) {
//            //hot_list.put(name.get(i),link.get(i));
//            System.out.println(name.get(i) + " " + link.get(i));
//        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static List<SongsEntity> getHotMusic() {
        topList = new ArrayList<>();
        Request request = new Request("http://music.163.com/discover/toplist?id=3778678");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new NetEaseCloudMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }

    public static List<SongsEntity> getRiseMusic() {
        topList = new ArrayList<>();
        Request request = new Request("http://music.163.com/discover/toplist?id=19723756");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new NetEaseCloudMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }

    public static List<SongsEntity> getNewMusic() {
        topList = new ArrayList<>();
        Request request = new Request("http://music.163.com/discover/toplist?id=3779629");
        request.setMethod(HttpConstant.Method.GET);
        Spider.create(new NetEaseCloudMusicCrawler()).addRequest(request).thread(5).run();
        return topList;
    }
}
